//
//  SZCMinesViewController.m
//  礼物说
//
//  Created by dllo on 16/4/19.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "SZCMinesViewController.h"

const CGFloat BackGroupHeight = 200;

@interface SZCMinesViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) BaseImageView *imgView;
@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) BaseLabel *lable;
@property (nonatomic, retain) UISwitch *switchs;
@property (nonatomic, retain) NSMutableArray *liwuArr;
@property (nonatomic, retain) NSMutableArray *gongLvArr;

@end

@implementation SZCMinesViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_imgView release];
    [_tableView release];
    [_lable release];
    [_switchs release];
    [_liwuArr release];
    [_gongLvArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人中心";
    // Do any additional setup after loading the view.
    
#pragma mark - TableView
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(375), kHEIGHTS(667) - 49) style:UITableViewStylePlain];
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
#pragma mark - ImageView
    self.imgView = [[BaseImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight, kWIDTH, BackGroupHeight)];
    [_tableView addSubview: _imgView];
    [_imgView release];
    _imgView.image = [UIImage imageNamed:@"358e605fdf0646c442a8d8485e2611b9.jpg"];
    
#pragma mark - 二维码
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(7, 7, 50, 50);
    [button setImage:[UIImage imageNamed:@"Android_QR_Code_48px_515614_easyicon.net.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

#pragma mark - 短信验证码
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(7, 60, 50, 50);
//    button1.backgroundColor = [UIColor redColor];
    [button1 setImage:[UIImage imageNamed:@"handcent_sms_ui_conversationexlist_32px_1135360_easyicon.net.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
}

- (void)buttonAction:(UIButton *)button {
    [[BeforeScanSingleton shareScan] ShowSelectedType:AliPayStyle WithViewController:self];
}

- (void)button1Action:(UIButton *)button {
    NSLog(@"333");
}


#pragma mark - TableView 方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    /** Cell 右侧箭头*/
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"喜欢的礼物";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"收藏数:%ld", (unsigned long) _liwuArr.count];
        return cell;
        
    }else if (indexPath.row == 1) {
        cell.textLabel.text = @"喜欢的攻略";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"收藏数: %ld", (unsigned long) _gongLvArr.count];
        return cell;
        
    }else if (indexPath.row == 2) {
        
        /** 设置右侧小三角为无*/
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        if (_switchs == nil) {
            self.switchs = [[[UISwitch alloc] initWithFrame:CGRectMake(kWIDTH - 60, 15, 50, 30)] autorelease];
        }
        /** 设置switch在onTint的颜色*/
        _switchs.tintColor = [UIColor whiteColor];
        _switchs.onTintColor = [UIColor colorWithRed:0.10 green:0.33 blue:0.65 alpha:1.00];
        [_switchs addTarget:self action:@selector(switAction:) forControlEvents:UIControlEventValueChanged];
        
        /** 获取上次夜间模式状态*/
        BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];
        
        /** 设置按钮状态*/
        _switchs.on = result;
        [cell addSubview:_switchs];
        cell.textLabel.text = @"夜间模式";
        return cell;
        
    }else {
        cell.textLabel.text = @"清除缓存";
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHEIGHTS(60);
}

#pragma mark - TableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        LikeGiftViewController *likeGiftVc = [[LikeGiftViewController alloc] init];
        likeGiftVc.array = _liwuArr;
        if (_liwuArr.count == 0) {
            [self createAlsert:@"提示" str1:@"还没有收藏" back:NO];
            
        }else {
            [self.navigationController pushViewController:likeGiftVc animated:YES];
        }
        [likeGiftVc release];
        
    }else if (indexPath.row == 1) {
        LIkeGongLvViewController *likegongLvVC = [[LIkeGongLvViewController alloc] init];
        likegongLvVC.array = _gongLvArr;
        
        if (_gongLvArr.count == 0) {
            [self createAlsert:@"提示" str1:@"还没有收藏" back:NO];
            
        }else {
            [self.navigationController pushViewController:likegongLvVC animated:YES];
        }
        [likegongLvVC release];
        
    }else if (indexPath.row == 3) {
        /**找到document文件夹 */
        NSString *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        /**在此文件夹下拼接文件名， 表示到什么文件， urlCache是网路请求的缓存文件 */
        NSString *netPath = [library stringByAppendingPathComponent:@"urlCache"];
        /**创建文件管理器 */
        NSFileManager *manager = [NSFileManager defaultManager];
        /**清除 */
        [manager removeItemAtPath:netPath error:nil];
        
        [[SDWebImageManager sharedManager].imageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
            /** 提示框*/
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示:" message:[NSString stringWithFormat: @"本次共清理了%.2fMB垃圾", (CGFloat)totalSize / 1024 / 1024] preferredStyle:UIAlertControllerStyleAlert];
            
            // 添加按钮
            // 参数1: 标题 参数2:样式 参数3:当点击按钮会自动进入参数3的block中进行处理
            UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
            /** 将添加按钮添加到提示框*/
            [alertC addAction:alertA];
            [self presentViewController:alertC animated:YES completion:nil];
        }];
        
        // 清除缓存
        [[SDWebImageManager sharedManager].imageCache clearMemory];
        
        // 清除磁盘缓存
        [[SDWebImageManager sharedManager].imageCache clearDisk];
        
        // ios7以后需要添加系统清除缓存
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    }

}

#pragma mark - 提示框
- (void)createAlsert:(NSString *)str str1:(NSString *)str1 back:(BOOL)back{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:str1 preferredStyle: UIAlertControllerStyleAlert];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    [UIView animateWithDuration:5 animations:^{
        
        [self dismissViewControllerAnimated:YES completion:^{
            
            if (back) {
            
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
    }];
}

#pragma mark - 计算单个文件大小的方法
-(CGFloat)fileSizeAtPath:(NSString *)path
{
    //创建一个文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    return 0;
}

#pragma mark - swit点击方法
- (void)switAction:(UISwitch *)swit {
    
    if (swit.on == YES) {
        NSLog(@"开启夜间模式");
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isNight"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];
        
    }else{
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isNight"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"day" object:nil];
        
        NSLog(@"关闭夜间模式");
    }

}

- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
    self.liwuArr = [NSMutableArray arrayWithArray:[[DatabaseHandle shareDataBase] selectTable]];
    
    self.gongLvArr = [NSMutableArray arrayWithArray:[[DatabaseHandle shareDataBase] selectTableGongLv]];
    
    [_tableView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
